package com.hyk.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyk.dto.ApiResult;
import com.hyk.entity.Role;
import com.hyk.entity.request.BaseRequest;
import com.hyk.entity.request.UserRoleRequest;
import com.hyk.service.IRoleService;
import com.hyk.utils.R;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hyk
 * @since 2021-03-16
 */
@RestController
@RequestMapping("/role")
@Api(tags = "角色管理")
@CrossOrigin
public class RoleController {

    @Resource
    private IRoleService roleService;

    @PostMapping("/list")
    public ApiResult list(@RequestBody BaseRequest request){
        IPage<Role> page = new Page<>(request.getPage(),request.getSize());
        IPage<Role> roleIPage = roleService.page(page);
        return R.ok(roleIPage);
    }

    @PostMapping("/add")
    public ApiResult add(@RequestBody Role role){
        role.setUpdateTime(new Date());
        if (role.getId() == null){
            role.setCreateTime(new Date());
        }
        roleService.saveOrUpdate(role);
        return R.ok();
    }

    @PostMapping("/delete")
    public ApiResult delete(@RequestBody Role role){
        roleService.removeById(role.getId());
        return R.ok();
    }

    @PostMapping("/distribution")
    public ApiResult distribution(@RequestBody UserRoleRequest userRoleRequest){
        roleService.distribution(userRoleRequest);
        return R.ok();
    }

}
